const app = getApp();
let plugin = requirePlugin('routePlan');
let key = 'RKEBZ-5AHRG-UESQB-ITOOU-5WSYJ-T4FCQ';  //使用在腾讯位置服务申请的key
let referer = 'yangsq109';   //调用插件的app的名称
let endPoint = JSON.stringify({  //终点
  'name': '武汉站',
  'latitude': 30.607346,
  'longitude': 114.424499
});
Page({ 
  data: { 
  },
  onLoad: function() {
  },
  open: function (e) {
    wx.navigateTo({
      url: '/pages/home/qqMap/qqMap'
    });
  },
  openPlugin: function(){
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },
  isSubscribe: function(){
    //需要订阅的消息模板，在微信公众平台手动配置获取模板ID
            // let message = 'bX0o6U1ltIaRoSl8AHBqFG3OGaCbLOWR9E_0rihaF8I' // 审批
            let message =  'u8I1TSurUb-jUI7zh13gACWCZX5xrqTcO06m6nvOG6w' // 申请
      //如果总是拒绝（subscriptionsSetting，2.10.1库才支持）
      if(this.versionCompare('2.10.1')){
          wx.getSetting({
              withSubscriptions: true,//是否同时获取用户订阅消息的订阅状态，默认不获取
              success: (res)=> {
                  console.log(res)
                  if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
                      res.subscriptionsSetting.itemSettings[message] == "reject"){
                      //打开设置去设置
                      this.openConfirm('检测到您没打开推送权限，是否去设置打开？')
                  }else {
                      wx.requestSubscribeMessage({
                          tmplIds: [message],
                          success: (res)=> {
                              if (res[message] == 'accept'){
                                  //用户允许
                              }
                          },
                          fail: (res)=> { console.info(res) },
                      })
                  }
              }
          })
      }
  },
  subscribeMessage: function(){
      var templateId = 'u8I1TSurUb-jUI7zh13gACWCZX5xrqTcO06m6nvOG6w' // 请假模板id
    //   var templateId = 'bX0o6U1ltIaRoSl8AHBqFG3OGaCbLOWR9E_0rihaF8I' // 审批通过模板id
    app.GET({
        url: '/wxConfig/subscribeMessage',
        data: {
            code: app.code,
            templateId: templateId,
            page: ''
        },
      }).then(res => {
        console.log(res)
        if (res.msg === 'success') {
          
        } else {
          console.log(res)
        }
      })
  },

  scan() {
    wx.scanCode({
     success: (res) => {
      console.log(res);
    //  this.setData({
    //   img: res.result
    //  })
     },
     fail: (res) => {
      console.log(res);
     }
    })
  },
  //打开设置
  openConfirm: function(message) {
    wx.showModal({
        content: message,
        confirmText: "确认",
        cancelText: "取消",
        success: (res) => {
            //点击“确认”时打开设置页面
            if (res.confirm) {
                wx.openSetting({
                    success: (res) => {
                        console.log(res.authSetting)
                    },
                    fail: (error) => {
                        console.log(error)
                    }
                })
            } else {
                console.log('用户点击取消')
            }
        }
    });
  },
  //基础库版本比较
  versionCompare: function(v) {
      const version = wx.getSystemInfoSync().SDKVersion
      if (app.compareVersion(version, v) >= 0) {
          return true
      } else {
          return false
      }
  }
})